package Proj_pack;

import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;


public class Mash {
	private final int cap;
	private ConcurrentLinkedQueue<Knoten> kList  = new ConcurrentLinkedQueue<Knoten>();
	public Lock lock = new ReentrantLock();
	
	public Mash(int cap){
		this.cap = cap;
	}
	
	public Mash(int cap, boolean b){
		this.cap = cap;
		lock.lock();
	}
	
	public void add(Knoten k){
		lock.lock();
		try{
		kList.add(k);
		k.setMash(this);
		}finally{
			lock.unlock();
		}
	}

	public ConcurrentLinkedQueue<Knoten> getkList() {
		lock.lock();
		try{
			return kList;
		}finally{
			lock.unlock();
		}
	}

	
	public int getCap() {
		return cap;
	}
	

	public int[] getSplitter() {
		lock.lock();
		try{
			int[] splitter = new int[getkList().size()];
			int i = 0;
			for(Knoten k : getkList()){
				splitter[i]=k.getId();
				i++;
			}
			return splitter;
		}finally{
			lock.unlock();
		}

	}

	
	public void removeKnoten(Knoten k){
		lock.lock();
		try{
		this.kList.remove(k);
//		k.setMash(null);
		}finally{
			lock.unlock();
		}
	}
	

	@Override
	public String toString(){

		StringBuffer sb = new StringBuffer();
//		sb.append("\n");
		for (Knoten k : kList){
			sb.append("" + k + "");
		}
		return sb.toString();
	}
	

//	@Override
//	public int hashCode() {
//		final int prime = 31;
//		int result = 1;
//		result = prime * result + cap;
//		result = prime * result + ((kList == null) ? 0 : kList.hashCode());
//		return result;
//	}
//
//	@Override
//	public boolean equals(Object obj) {
//		if (this == obj)
//			return true;
//		if (obj == null)
//			return false;
//		if (getClass() != obj.getClass())
//			return false;
//		Mash other = (Mash) obj;
//		if (cap != other.cap)
//			return false;
//		if (kList == null) {
//			if (other.kList != null)
//				return false;
//		} else if (!kList.equals(other.kList))
//			return false;
//		return true;
//	}
	
}
